﻿#include <iostream>
#include <fstream>
#include <Windows.h>
#include <iomanip>
#include <math.h>
#include "TranParCheck.h"
#include <BPAFilesIO.h>
#include <BaseCommon.h>
#include"MathFunction.h"

#define AttributeNum 9;

using namespace JGG;

TranParCheck::TranParCheck(BPA::BPAFilesIO::DatParse* datParse, const char* database, int caseid)
{
    this->datParse = datParse;
    this->database = database;
    this->caseid = caseid;

    inst = new BPA::BPAFilesIO::GetDatFromDb(datParse, database, caseid);
}
TranParCheck::~TranParCheck()
{
	TranInfUnComplet.clear();  //存储参数不完整的变压器
	TranProblem.clear();	   //存储参数存在问题的变压器

    if (inst)
    {
        delete inst;
        inst = nullptr;
    }
};
void TranParCheck::TranPar_2_Check()
{
	TRANSFORMVector TransListc = inst->GetTran2List();
	BUSVector BusListc = inst->GetBusList();
	//BpaData2->BpaDatSaveToDatBase();
	SqliteHelperDataBase db;
	SqliteHelperBuffer bufSQL;
	SqliteHelperBuffer bufSQL1;
	int CaseIDtmp = 0;
	string FileName = inst->GetFileName();
	double MaxXpu;
	double MinXpu;
	db.open(database);
	SqliteHelperQuery query1 = db.execQuery("select * from FileList;");
	while (!query1.eof())
	{
		if (query1.getStringField(0) == FileName)
		{
			CaseIDtmp = query1.getIntField(2);
		}
		query1.nextRow();
	}
	bufSQL.format("select max(Xpu),min(Xpu) from Transform_2List where CaseID <> (%d);", CaseIDtmp);
	SqliteHelperQuery query2 = db.execQuery(bufSQL);
	bufSQL.format("select * from Transform_2List where CaseID <> (%d);",CaseIDtmp);
	SqliteHelperQuery query3 = db.execQuery(bufSQL);
	if (!query3.eof())
	{
		MaxXpu = query2.getFloatField(0);
		MinXpu = query2.getFloatField(1);
		int transize;
		transize = TransListc.size();
		for (int i = 0; i < transize; i++)
		{
			if ((TransListc[i].Xpu == 999999 || TransListc[i].Xpu == 0) && (TransListc[i].Volume != 999999))
			{
				TransListc[i].TranPro = "缺少变压器的漏抗值";
				TranInfUnComplet.push_back(TransListc[i]);
				//cout << "ERROR:" << "  " << BusListc[TransListc[i].StartNum - 1].BusName << "  " << TransListc[i].StartVol << "  " << BusListc[TransListc[i].EndNum - 1].BusName
				//<< "  " << TransListc[i].EndVol << "  " << "缺少变压器的漏抗值" << endl;
			}
			if ((TransListc[i].Xpu == 999999 || TransListc[i].Xpu == 0) && (TransListc[i].Volume == 999999))
			{
				TransListc[i].TranPro = "缺少变压器的漏抗值和容量值";
				TranInfUnComplet.push_back(TransListc[i]);
				//cout << "ERROR:" << "  " << BusListc[TransListc[i].StartNum - 1].BusName << "  " << TransListc[i].StartVol << "  " << BusListc[TransListc[i].EndNum - 1].BusName
				//<< "  " << TransListc[i].EndVol << "  " << "缺少变压器的漏抗值和容量值" << endl;
			}
			if ((TransListc[i].Xpu != 999999 && TransListc[i].Xpu != 0) && (TransListc[i].Volume == 999999))
			{
				TransListc[i].TranPro = "缺少变压器的容量值";
				TranInfUnComplet.push_back(TransListc[i]);
				//cout << "ERROR:" << "  " << BusListc[TransListc[i].StartNum - 1].BusName << "  " << TransListc[i].StartVol << "  " << BusListc[TransListc[i].EndNum - 1].BusName
				//<< "  " << TransListc[i].EndVol << "  " << "缺少变压器的容量值" << endl;
			}
			if (TransListc[i].Xpu != 999999 && TransListc[i].Xpu < MinXpu)
			{
				TransListc[i].TranPro = "变压器的漏抗值可能存在问题,漏抗值小于历史最小值";
				TranProblem.push_back(TransListc[i]);
				//cout << "ERROR:" << "  " << BusListc[TransListc[i].StartNum - 1].BusName << "  " << TransListc[i].StartVol << "  " << BusListc[TransListc[i].EndNum - 1].BusName
				//<< "  " << TransListc[i].EndVol << "  " << "变压器的漏抗值可能存在问题" << endl;
			}
			if (TransListc[i].Xpu != 999999 && TransListc[i].Xpu > MaxXpu)
			{
				TransListc[i].TranPro = "变压器的漏抗值可能存在问题,漏抗值大于历史最大值";
				TranProblem.push_back(TransListc[i]);
				//cout << "ERROR:" << "  " << BusListc[TransListc[i].StartNum - 1].BusName << "  " << TransListc[i].StartVol << "  " << BusListc[TransListc[i].EndNum - 1].BusName
				//<< "  " << TransListc[i].EndVol << "  " << "变压器的漏抗值可能存在问题" << endl;
			}
		}
	}
	else
	{
		vector <double> Voltage;
		vector <double> Average;
		vector <double> Variance;
		TRANSFORM Trantmp;
		BPA::BPAFilesIO::GetInitConfigFromDb Inf(database);
		vector <double> Parameterc;
		Parameterc = Inf.GetParameter();
		double Sigema = Parameterc[7];

		SqliteHelperQuery query = db.execQuery("select Vset1,avg(Xpu) from Transform_2List GROUP BY Vset1;");
		while (!query.eof())
		{
			int Flag = 0;
			for (int i = 0; i < query.numFields(); i++)
			{
				if (Flag == 0)
				{
					Voltage.push_back(query.getFloatField(i));
					//cout << query.getFloatField(i) << endl;
					Flag++;
				}
				else if (Flag == 1)
				{
					Average.push_back(query.getFloatField(i));
				}
			}
			query.nextRow();
		}
		int VoltageSize = Voltage.size();
		for (int i = 0; i < VoltageSize; i++)
		{
			double Sumsquare = 0;
			bufSQL.format("select count(*) from Transform_2List where Vset1 = (%f) AND Xpu <> 'NULL' AND CaseID = (%d);", Voltage[i], CaseIDtmp);
			int rownum = db.execScalar(bufSQL);
			bufSQL1.format("select * from Transform_2List where Vset1 = (%f) AND Xpu <> 'NULL' AND CaseID = (%d);", Voltage[i], CaseIDtmp);
			SqliteHelperQuery query = db.execQuery(bufSQL1);
			//cout << query.eof() << endl;
			while (!query.eof())
			{
				double Xputmp = query.getFloatField(10);
				//std::cout << Xputmp << std::endl;
				Sumsquare = Sumsquare + (Xputmp - Average[i])*(Xputmp - Average[i]);
				query.nextRow();
			}
			double Avgsuare = sqrt(Sumsquare / rownum);
			//std::cout << query.eof() << std::endl;
			SqliteHelperQuery query1 = db.execQuery(bufSQL1);
			while (!query1.eof())
			{
				double Xputmp = query1.getFloatField(10);
				if ((Xputmp<(Average[i] - Sigema*Avgsuare)) || (Xputmp>(Average[i] + Sigema*Avgsuare)))
				{
					Trantmp.StartNum = query1.getIntField(3);
					Trantmp.EndNum = query1.getIntField(4);
					Trantmp.Volume = query1.getFloatField(6);
					Trantmp.Rpu = query1.getFloatField(7);
					Trantmp.Xpu = query1.getFloatField(10);
					Trantmp.Bpu = query1.getFloatField(16);
					Trantmp.StartVol = query1.getFloatField(19);
					Trantmp.EndVol = query1.getFloatField(22);
					Trantmp.TranPro = "变压器的漏抗值可能存在问题";
					TranProblem.push_back(Trantmp);
				}
				query1.nextRow();
			}
		}
		Voltage.clear();
		Average.clear();
		Variance.clear();
		Parameterc.clear();
	}
	db.close();
	TransListc.clear();
	BusListc.clear();
}

//void TranJudgeMent::TranParameter_3_Judgement(const char* database)
//{
//}

void TranParCheck::Tran2Dat()
{
	BUSVector BusListc = inst->GetBusList();
	string FileName = inst->GetFileName();
	int size = TranInfUnComplet.size();
	int sizec = TranProblem.size();
	std::ofstream FunctionResult((FileName + " - ProblemTran" + ".dat").c_str(), ios::out);
	setiosflags(ios::right);
	FunctionResult << "变压器参数可能存在问题列表：" << endl << endl;
	FunctionResult << "始端母线" << "  " << setw(8) << "末端母线" << "   " << setw(8) << "始端电压" << "  " << setw(8) << "末端电压" << "  " << setw(8) << "变压器容量" << "  " << setw(8) << "变压器漏抗" << "   " << setw(8) <<
		"变压器问题";
	FunctionResult << endl;
	for (int i = 0; i < sizec; i++)
	{
		FunctionResult << BusListc[TranProblem[i].StartNum - 1].BusName << "  " << setw(8) << BusListc[TranProblem[i].EndNum - 1].BusName << "   " << setw(8) << TranProblem[i].StartVol << "  " << setw(8) << TranProblem[i].EndVol << "  " << setw(8) << TranProblem[i].Volume << "  " << setw(8) << TranProblem[i].Xpu << "   " << setw(8) <<
			TranProblem[i].TranPro;
		FunctionResult << endl;
	}
	FunctionResult << "变压器参数填写不全的列表：" << endl << endl;
	FunctionResult << "始端母线" << "  " << setw(8) << "末端母线" << "   " << setw(8) << "始端电压" << "  " << setw(8) << "末端电压" << "  " << setw(8) << "变压器容量" << "  " << setw(8) << "变压器漏抗" << "   " << setw(8) <<
		"变压器问题";
	FunctionResult << endl;
	for (int i = 0; i < size; i++)
	{
		FunctionResult << BusListc[TranInfUnComplet[i].StartNum - 1].BusName.c_str() << "  " << setw(8) << BusListc[TranInfUnComplet[i].EndNum - 1].BusName.c_str() << "   " << setw(8) << TranInfUnComplet[i].StartVol << "  " << setw(8) << TranInfUnComplet[i].EndVol << "  " << setw(8) << TranInfUnComplet[i].Volume << "  " << setw(8) << TranInfUnComplet[i].Xpu << "   " << setw(8) <<
			TranInfUnComplet[i].TranPro;
		FunctionResult << endl;
	}
	FunctionResult.close();
	BusListc.clear();
}
void TranParCheck::Tran2Db()
{
	//const char* database = "./PowerFlow.db";
	int TranInfUnCompletNum, TranProblemNum;
	TranInfUnCompletNum = TranInfUnComplet.size();
	TranProblemNum = TranProblem.size();
	SqliteHelperDataBase db;
	SqliteHelperBuffer bufSQL;
	db.open(database);
	char buf[500];
	std::memset(buf, 0, sizeof(buf));
	sprintf_s(buf, "delete from  TranInfUnCompletList where CaseID = (%d);", caseid);
	db.execDML(buf);
	std::memset(buf, 0, sizeof(buf));
	sprintf_s(buf, "delete from  Tran2ProblemList where CaseID = (%d);", caseid);
	db.execDML(buf);
	std::memset(buf, 0, sizeof(buf));
	sprintf_s(buf, "delete from  Tran3ProblemList where CaseID = (%d);", caseid);
	db.execDML(buf);
	std::memset(buf, 0, sizeof(buf));
	sprintf_s(buf, "delete from  TransBadDataList where CaseID = (%d);", caseid);
	db.execDML(buf);
	db.execDML("begin transaction;");
	BUSVector BusListc = inst->GetBusList();
	for (int i = 0; i < TranInfUnCompletNum; i++)
	{
		char buf[400];
		sprintf_s(buf, "insert into TranInfUnCompletList(CaseID,TranID,StartBus,StartVol,EndBus,EndVol,TranVolum,TranX,TranPro) values (%d,%d,'%s',%f,'%s',%f,%f,%f,'%s');",
			caseid, i + 1, Unicode2Utf8(str2Wstr(BusListc[TranInfUnComplet[i].StartNum - 1].BusName)).c_str(), TranInfUnComplet[i].StartVol, Unicode2Utf8(str2Wstr(BusListc[TranInfUnComplet[i].EndNum - 1].BusName)).c_str(), TranInfUnComplet[i].EndVol,
			TranInfUnComplet[i].Volume, TranInfUnComplet[i].Xpu, Unicode2Utf8(str2Wstr(TranInfUnComplet[i].TranPro)).c_str());
		db.execDML(buf);
	}
	for (int i = 0; i < TranProblemNum; i++)
	{
		
		char buf[400];
		sprintf_s(buf, "insert into Tran2ProblemList(CaseID,TranID,StartBus,StartVol,EndBus,EndVol,TranVolum,TranX,TranPro,ShuntFlag) values (%d,%d,'%s',%f,'%s',%f,%f,%f,'%s','%s');",
			caseid, i + 1, Unicode2Utf8(str2Wstr(BusListc[TranProblem[i].StartNum - 1].BusName)).c_str(), TranProblem[i].StartVol, Unicode2Utf8(str2Wstr(BusListc[TranProblem[i].EndNum - 1].BusName)).c_str(), TranProblem[i].EndVol,
			TranProblem[i].Volume, TranProblem[i].Xpu, Unicode2Utf8(str2Wstr(TranProblem[i].TranPro)).c_str(), TranProblem[i].ShuntFlag.c_str());
		db.execDML(buf);
	
	}
	char buf1[200];
	sprintf_s(buf1, "update TranInfUnCompletList set TranVolum=NULL where TranVolum=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update TranInfUnCompletList set TranX=NULL where TranX=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update Tran2ProblemList set TranVolum=NULL where TranVolum=999999");
	db.execDML(buf1);
	sprintf_s(buf1, "update Tran2ProblemList set TranX=NULL where TranX=999999");
	db.execDML(buf1);
	
	db.execDML("commit transaction;");
	db.close();
}
string TranParCheck::Unicode2Utf8(const std::wstring& widestring)
{  
	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);  
	if (utf8size == 0)  
	{  
		throw std::exception("Error in conversion.");  
	}  
	std::vector<char> resultstring(utf8size);  
	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);  
	if (convresult != utf8size)  
	{  
		throw std::exception("La falla!");  
	}  
	return std::string(&resultstring[0]);  
}  
bool TranParCheck::StringToWString(const std::string &str, std::wstring &wstr)
{
	int nLen = (int)str.length();    
	wstr.resize(nLen,L' ');
	int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
	if (nResult == 0)
	{
		return FALSE;
	}
	return TRUE;
}
wstring TranParCheck::str2Wstr(string str)
{
	wstring Wstr;
	StringToWString(str,Wstr);
	return Wstr;
}


vector<vector<double>> TranParCheck::ReadDataFromDatabase()
{
	//本函数的功能是从数据库中读取三绕组变压器信息
	JGG::SqliteHelperDataBase db;
	JGG::SqliteHelperBuffer bufSQL;

	TriWinTransList.clear();
	db.open(database);//打开数据库
	bufSQL.format("select * from Transform_3List where CaseID=(%d)", caseid);
	JGG::SqliteHelperTable Transform_3List = db.getTable(bufSQL);

	int row = Transform_3List.numRows();//二维数组行数
	int column = AttributeNum;//二维数组列数,共9个属性，即（X3 X2 X1 S3 S2 S1 Vol3 Vol2 Vol1）
	vector<vector<double>> TriWinTransMatrix(row, vector<double>(column));//申请一个二维数组，存储三绕组变压器信息聚类特征信息，方便后面计算

	for (unsigned int i = 0; i < Transform_3List.numRows(); i++)
	{
		Transform_3List.setRow(i);//定位至表中i行
		TriWinTransTemp.TranID = atoi(Transform_3List.getStringField(2).c_str());
		TriWinTransTemp.StartNum = atoi(Transform_3List.getStringField(3).c_str());
		TriWinTransTemp.MiddleNum = atoi(Transform_3List.getStringField(4).c_str());
		TriWinTransTemp.EndNum = atoi(Transform_3List.getStringField(5).c_str());
		TriWinTransTemp.StartName = Transform_3List.getStringField(25);
		TriWinTransTemp.MiddleName = Transform_3List.getStringField(26);
		TriWinTransTemp.EndName = Transform_3List.getStringField(27);

		TriWinTransTemp.X3 = atof(Transform_3List.getStringField(12).c_str());
		TriWinTransMatrix[i][0] = TriWinTransTemp.X3;

		TriWinTransTemp.X2 = atof(Transform_3List.getStringField(13).c_str());
		TriWinTransMatrix[i][1] = TriWinTransTemp.X2;

		TriWinTransTemp.X1 = atof(Transform_3List.getStringField(14).c_str());
		TriWinTransMatrix[i][2] = TriWinTransTemp.X1;

		TriWinTransTemp.S3 = atof(Transform_3List.getStringField(6).c_str());
		TriWinTransMatrix[i][3] = TriWinTransTemp.S3;

		TriWinTransTemp.S2 = atof(Transform_3List.getStringField(7).c_str());
		TriWinTransMatrix[i][4] = TriWinTransTemp.S2;

		TriWinTransTemp.S1 = atof(Transform_3List.getStringField(8).c_str());
		TriWinTransMatrix[i][5] = TriWinTransTemp.S1;

		TriWinTransTemp.Vol3 = atof(Transform_3List.getStringField(21).c_str());
		TriWinTransMatrix[i][6] = TriWinTransTemp.Vol3;

		TriWinTransTemp.Vol2 = atof(Transform_3List.getStringField(22).c_str());
		TriWinTransMatrix[i][7] = TriWinTransTemp.Vol2;

		TriWinTransTemp.Vol1 = atof(Transform_3List.getStringField(23).c_str());
		//atoi函数不好，可以修改为str2oi,参考basecommon库
		TriWinTransMatrix[i][8] = TriWinTransTemp.Vol1;

        TriWinTransTemp.VirtualNodeName = Transform_3List.getStringField(28);
        TriWinTransTemp.VirtualNodeNum = atoi(Transform_3List.getStringField(29).c_str());

		TriWinTransList.push_back(TriWinTransTemp);
	}
	db.close();
	return TriWinTransMatrix;
}

vector<vector<double>>  TranParCheck::Standardization(vector<vector<double>>  TriWinTransMatrix)
{
	//本函数的功能是对数据进行z-score标准化
	int column = AttributeNum;
	vector<vector<double>> TriWinTransMatrixStd = TriWinTransMatrix;//初始化
	vector <double> AttributeTemp;//用来存储某个属性变量的所有数据，即矩阵的某一列
	MathFunction MF;//调用自定义数学函数
	for (unsigned int i = 0; i < column; i++)
	{
		AttributeTemp.clear();//先清空
		for (unsigned int j = 0; j < TriWinTransMatrix.size(); j++)
		{
			AttributeTemp.push_back(TriWinTransMatrix[j][i]);
		}
		double mean = MF.getMean(AttributeTemp);//求均值 
		double stdev = MF.getStDev(AttributeTemp);//标准差

		//根据均值和标准差进行z-score标准化
		for (unsigned int j = 0; j < TriWinTransMatrix.size(); j++)
		{
			TriWinTransMatrixStd[j][i] = (TriWinTransMatrixStd[j][i] - mean) / stdev;
		}
	}
	return TriWinTransMatrixStd;
}

vector<double> TranParCheck::getClassicVector(vector<vector<double>> TriWinTransMatrixStd, vector<vector<double>> CorrMatrix, vector<double>cidx)
{
	//本函数的作用是获取每一个类中的典型向量的位置
	MathFunction MF;
	int ClusterNum = MF.getMax(cidx);
	vector<double> ClassicVectorID;
	ClassicVectorID.clear();
	for (unsigned int i = 1; i < ClusterNum + 1; i++)
	{
		vector <double> IDList = MF.FindID(cidx, "==", i);
		//构建每一个子类的相关系数矩阵
		vector<vector<double>> subCorrMatrix(IDList.size(), vector<double>(IDList.size()));
		for (unsigned int m = 0; m < IDList.size(); m++)
		{
			for (unsigned int n = m; n < IDList.size(); n++)
			{
				int CorrMatrix_m = IDList[m];
				int CorrMatrix_n = IDList[n];
				double CorrMatrixTemp = CorrMatrix[CorrMatrix_m][CorrMatrix_n];
				subCorrMatrix[m][n] = CorrMatrixTemp;
				subCorrMatrix[n][m] = subCorrMatrix[m][n];
			}
		}
		//求每一个子类的相关系数矩阵的均值，并在这些均值中找出最大值对应的位置
		vector <double> AvgCorr;
		AvgCorr.clear();
		for (unsigned int j = 0; j < IDList.size(); j++)
		{
			vector <double>subCorrMatrixTemp = subCorrMatrix[j];
			subCorrMatrixTemp.erase(subCorrMatrixTemp.begin() + j);
			AvgCorr.push_back(MF.getMean(subCorrMatrixTemp));
		}
		vector <double>ClassicVectorIDTemp = MF.FindID(AvgCorr, "==", MF.getMax(AvgCorr));
		ClassicVectorID.push_back(ClassicVectorIDTemp[0]);
	}
	return ClassicVectorID;
}

void TranParCheck::TransBadDataIdAllProgress()
{
    BPA::BPAFilesIO::GetInitConfigFromDb Inf(database);
    vector <double> Parameterc;
    Parameterc = Inf.GetParameter();
    double Eps = Parameterc[9];

    //double Eps = 0.87;

	SqliteHelperDataBase db;
	SqliteHelperBuffer bufSQL;
	MathFunction MF;//调用自定义数学函数

	//首先读取数据库中三然组变压器信息
	vector<vector<double>> TriWinTransMatrix = ReadDataFromDatabase();
	//对数据标准化
	vector<vector<double>> TriWinTransMatrixStd = Standardization(TriWinTransMatrix);
	//计算相关系数矩阵
	vector<vector<double>> CorrMatrix = MF.getCorrMatrix(TriWinTransMatrixStd);
	//DBSCAN聚类分析，默认minpts=2;
	vector <double> cidx = MF.DBSCAN_Corr(CorrMatrix, Eps, 2);
	//典型向量ID的确定
	vector<double> ClassicVectorID = getClassicVector(TriWinTransMatrixStd, CorrMatrix, cidx);
	//噪声向量ID的确定
	vector<double> NoiseVectorID = MF.FindID(cidx, "==", -1);
	//确定每一个噪声向量的离群系数，并写入数据库
	vector<double> OutlierCoefficient;//确定每一个噪声向量的离群系数
	db.open(database);
	char buf[500];
	sprintf_s(buf, "delete from  TransBadDataList where CaseID = (%d);", caseid);
	db.execDML(buf);
	db.execDML("begin transaction;");
	for (unsigned int i = 0; i < NoiseVectorID.size(); i++)
	{
		vector <double> CorrOfNoiseAndClassic;
		CorrOfNoiseAndClassic.clear();
		for (unsigned int j = 0; j < ClassicVectorID.size(); j++)
		{
			CorrOfNoiseAndClassic.push_back(CorrMatrix[NoiseVectorID[i]][ClassicVectorID[j]]);
		}
		double OutlierCoefficientTemp = MF.getMax(CorrOfNoiseAndClassic);
		OutlierCoefficient.push_back(OutlierCoefficientTemp);
		//cout << caseid << "," << TriWinTransList[NoiseVectorID[i]].StartNum << "," << TriWinTransList[NoiseVectorID[i]].MiddleNum
			//<< "," << TriWinTransList[NoiseVectorID[i]].EndNum<<","<<TriWinTransList[NoiseVectorID[i]].VirtualNodeNum 
			//<< "," << TriWinTransList[NoiseVectorID[i]].VirtualNodeNum << "," << TriWinTransList[NoiseVectorID[i]].VirtualNodeName
			//<< "," << TriWinTransList[NoiseVectorID[i]].StartName << "," << TriWinTransList[NoiseVectorID[i]].MiddleName
			//<< "," << TriWinTransList[NoiseVectorID[i]].EndName << "," << TriWinTransList[NoiseVectorID[i]].X3
			//<< "," << TriWinTransList[NoiseVectorID[i]].X2 << "," << TriWinTransList[NoiseVectorID[i]].X1
			//<< "," << TriWinTransList[NoiseVectorID[i]].S3 << "," << TriWinTransList[NoiseVectorID[i]].S2
			//<< "," << TriWinTransList[NoiseVectorID[i]].S1 << "," << TriWinTransList[NoiseVectorID[i]].Vol3
			//<< "," << TriWinTransList[NoiseVectorID[i]].Vol2 << "," << TriWinTransList[NoiseVectorID[i]].Vol1
			//<< "," << OutlierCoefficientTemp<< endl;
		bufSQL.format("insert into TransBadDataList(CaseID ,StartNum , MiddleNum , EndNum,VirtualNodeNum, VirtualNodeName,StartName ,MiddleName ,EndName ,X3 ,X2 ,X1 ,S3 ,S2 ,S1 ,Vol3 ,Vol2 ,Vol1 ,OutlierCoefficient )values(%d,%d,%d,%d,%d,'%s','%s','%s','%s',%f,%f,%f,%f,%f,%f,%f,%f,%f,%f)",
			caseid, TriWinTransList[NoiseVectorID[i]].StartNum, TriWinTransList[NoiseVectorID[i]].MiddleNum, TriWinTransList[NoiseVectorID[i]].EndNum, TriWinTransList[NoiseVectorID[i]].VirtualNodeNum,
			TriWinTransList[NoiseVectorID[i]].VirtualNodeName.c_str(), TriWinTransList[NoiseVectorID[i]].StartName.c_str(), TriWinTransList[NoiseVectorID[i]].MiddleName.c_str(), TriWinTransList[NoiseVectorID[i]].EndName.c_str(),
			TriWinTransList[NoiseVectorID[i]].X3, TriWinTransList[NoiseVectorID[i]].X2, TriWinTransList[NoiseVectorID[i]].X1,
			TriWinTransList[NoiseVectorID[i]].S3, TriWinTransList[NoiseVectorID[i]].S2, TriWinTransList[NoiseVectorID[i]].S1,
			TriWinTransList[NoiseVectorID[i]].Vol3, TriWinTransList[NoiseVectorID[i]].Vol2, TriWinTransList[NoiseVectorID[i]].Vol1,
			OutlierCoefficientTemp);
		db.execDML(bufSQL);
	}
	db.execDML("commit transaction;");
	db.close();
}






